home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / dir_read.c < prev    next >
C/C++ Source or Header  |  1992-09-10  |  3KB  |  154 lines

  1. #include <stdio.h>
  2. #include "msdos.h"
  3.  
  4. long dir_chain[MAX_DIR_SECS];        /* chain of sectors in directory */
  5. unsigned char *dir_buf;            /* the directory buffer */
  6. int dir_dirty;                /* is the buffer dirty? */
  7.  
  8. extern int dir_len, dir_start, clus_size, dir_entries, fat_error;
  9. extern unsigned int last_fat;
  10.  
  11. /*
  12.  * Read a directory entry, return a pointer a static structure.
  13.  */
  14.  
  15. struct directory *
  16. dir_read(num)
  17. int num;
  18. {
  19.     char *memcpy();
  20.     unsigned char *offset;
  21.     static struct directory dir;
  22.  
  23.     offset = dir_buf + (num * MDIR_SIZE);
  24.     memcpy((char *) &dir, (char *) offset, MDIR_SIZE);
  25.     return(&dir);
  26. }
  27.  
  28. /*
  29.  * Fill in the global variable dir_chain[].  Argument is the starting
  30.  * cluster number.  Returns -1 on error.
  31.  */
  32.  
  33. int
  34. fill_chain(num)
  35. unsigned int num;
  36. {
  37.     register int i, length;
  38.     unsigned int next, fat_decode();
  39.     unsigned char *offset;
  40.     char *realloc();
  41.     void perror(), exit(), disk_read(), dir_flush();
  42.  
  43.     length = 0;
  44.     /* CONSTCOND */
  45.     while (1) {
  46.         dir_chain[length] = (long) (num - 2) * clus_size + dir_start + dir_len;
  47.         length++;
  48.                     /* sectors, not clusters! */
  49.         for (i = 1; i < clus_size; i++) {
  50.             dir_chain[length] = dir_chain[length - 1] + 1L;
  51.             length++;
  52.         }
  53.  
  54.         if (length >= MAX_DIR_SECS) {
  55.             fprintf(stderr, "fill_chain: directory too large\n");
  56.             return(-1);
  57.         }
  58.                     /* get next cluster number */
  59.         next = fat_decode(num);
  60.         if (next == 1) {
  61.             fprintf(stderr, "fill_chain: FAT problem\n");
  62.             fat_error++;
  63.             return(-1);
  64.         }
  65.                     /* end of cluster chain */
  66.         if (next >= last_fat)
  67.             break;
  68.         num = next;
  69.     }
  70.     if (dir_dirty)
  71.         dir_flush();
  72.                     /* fill the dir_buf */
  73.     dir_buf = (unsigned char *) realloc(dir_buf, (unsigned int) length * MSECTOR_SIZE);
  74.     if (dir_buf == NULL) {
  75.         perror("fill_chain: realloc");
  76.         exit(1);
  77.     }
  78.  
  79.     for (i = 0; i < length; i++) {
  80.         offset = dir_buf + (i * MSECTOR_SIZE);
  81.         disk_read(dir_chain[i], offset, MSECTOR_SIZE);
  82.     }
  83.  
  84.     dir_entries = length * 16;
  85.     return(0);
  86. }
  87.  
  88. /*
  89.  * Reset the global variable dir_chain[] to the root directory.
  90.  */
  91.  
  92. void
  93. reset_chain(code)
  94. int code;
  95. {
  96.     register int i;
  97.     char *malloc(), *realloc();
  98.     void disk_read(), dir_flush(), exit(), perror();
  99.  
  100.     if (dir_dirty)
  101.         dir_flush();
  102.  
  103.     for (i = 0; i < dir_len; i++)
  104.         dir_chain[i] = (long) dir_start + i;
  105.  
  106.     if (code == OLD)
  107.         dir_buf = (unsigned char *) realloc(dir_buf, (unsigned int) dir_len * MSECTOR_SIZE);
  108.     else
  109.         dir_buf = (unsigned char *) malloc((unsigned int) dir_len * MSECTOR_SIZE);
  110.     if (dir_buf == NULL) {
  111.         perror("reset_chain: malloc");
  112.         exit(1);
  113.     }
  114.     disk_read((long) dir_start, dir_buf, dir_len * MSECTOR_SIZE);
  115.  
  116.     dir_entries = dir_len * 16;
  117.     return;
  118. }
  119.  
  120. /*
  121.  * Get rid of spaces in an MSDOS 'raw' name (one that has come from the
  122.  * directory structure) so that it can be used for regular expression
  123.  * matching with a unix filename.  Also used to 'unfix' a name that has
  124.  * been altered by dos_name().  Returns a pointer a static buffer.
  125.  */
  126.  
  127. char *
  128. unix_name(name, ext)
  129. unsigned char *name, *ext;
  130. {
  131.     char *s, tname[9], text[4], *strcpy(), *strcat(), *strchr();
  132.     char *strncpy();
  133.     static char ans[13];
  134.  
  135.     strncpy(tname, (char *) name, 8);
  136.     tname[8] = '\0';
  137.     if (s = strchr(tname, ' '))
  138.         *s = '\0';
  139.  
  140.     strncpy(text, (char *) ext, 3);
  141.     text[3] = '\0';
  142.     if (s = strchr(text, ' '))
  143.         *s = '\0';
  144.  
  145.     if (*text) {
  146.         strcpy(ans, tname);
  147.         strcat(ans, ".");
  148.         strcat(ans, text);
  149.     }
  150.     else
  151.         strcpy(ans, tname);
  152.     return(ans);
  153. }
  154.